home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0115_Pcx Viewer!.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  4KB  |  147 lines

  1.  
  2. Uses Crt;
  3. { Sample program to display a 320x200x256 PCX in
  4.   mode 13h.  PCX source copied from MCGA07, a MCGA
  5.   graphics unit written by James Cook in his MCGA
  6.   programming tutorial on Quantum Leap BBS }
  7.  
  8. TYPE
  9.   TPalette = array[0..767] of Byte;
  10.   PalettePtr = ^TPalette;
  11. { PCX stuff }
  12.   PCXHeaderPtr=  ^PCXHeader;
  13.   PCXHeader   =  record
  14.                    Signature      :  Char;
  15.                    Version        :  Char;
  16.                    Encoding       :  Char;
  17.                    BitsPerPixel   :  Char;
  18.                    XMin,YMin,
  19.                    XMax,YMax      :  Integer;
  20.                    HRes,VRes      :  Integer;
  21.                    Palette        :  Array [0..47] of byte;
  22.                    Reserved       :  Char;
  23.                    Planes         :  Char;
  24.                    BytesPerLine   :  Integer;
  25.                    PaletteType    :  Integer;
  26.                    Filler         :  Array [0..57] of byte;
  27.                  end;
  28.  
  29. Procedure ExtractLineASM (BytesWide:Integer;Var Source,Dest:Pointer);
  30. var
  31.   DestSeg,
  32.   DestOfs,
  33.   SourceSeg,
  34.   SourceOfs   :  Word;
  35. begin
  36.   SourceSeg := Seg (Source^);
  37.   SourceOfs := Ofs (Source^);
  38.   DestSeg   := Seg (Dest^);
  39.   DestOfs   := Ofs (Dest^);
  40.  
  41.   asm
  42.     push  ds
  43.     push  si
  44.  
  45.     cld
  46.  
  47.     mov   ax,DestSeg
  48.     mov   es,ax
  49.     mov   di,DestOfs     { es:di -> destination pointer }
  50.     mov   ax,SourceSeg
  51.     mov   ds,ax
  52.     mov   si,SourceOfs   { ds:si -> source buffer }
  53.  
  54.     mov   bx,di
  55.     add   bx,BytesWide   { bx holds position to stop for this row }
  56.     xor   cx,cx
  57.  
  58.   @@GetNextByte:
  59.     cmp   bx,di          { are we done with the line }
  60.     jbe   @@ExitHere
  61.  
  62.     lodsb                { al contains next byte }
  63.  
  64.     mov   ah,al
  65.     and   ah,0C0h
  66.     cmp   ah,0C0h
  67.  
  68.     jne    @@SingleByte
  69.                          { must be a run of bytes }
  70.     mov   cl,al
  71.     and   cl,3Fh
  72.     lodsb
  73.     rep   stosb
  74.     jmp   @@GetNextByte
  75.  
  76.   @@SingleByte:
  77.     stosb
  78.     jmp   @@GetNextByte
  79.  
  80.   @@ExitHere:
  81.     mov   SourceSeg,ds
  82.     mov   SourceOfs,si
  83.     mov   DestSeg,es
  84.     mov   DestOfs,di
  85.  
  86.     pop   si
  87.     pop   ds
  88.   end;
  89.  
  90.   Source := Ptr (SourceSeg,SourceOfs);
  91.   Dest   := Ptr (DestSeg,DestOfs);
  92. end;
  93.  
  94. Procedure DisplayPCX (X,Y:Integer;Buf:Pointer);
  95. var
  96.   I,NumRows,
  97.   BytesWide   :  Integer;
  98.   Header      :  PCXHeaderPtr;
  99.   DestPtr     :  Pointer;
  100.   Offset      :  Word;
  101.  
  102. begin
  103.   Header    := Ptr (Seg(Buf^),Ofs(Buf^));
  104.   Buf       := Ptr (Seg(Buf^),Ofs(Buf^)+128);
  105.   Offset    := Y * 320 + X;
  106.   NumRows   := Header^.YMax - Header^.YMin + 1;
  107.   BytesWide := Header^.XMax - Header^.XMin + 1;
  108.   If Odd (BytesWide) then Inc (BytesWide);
  109.  
  110.   For I := 1 to NumRows do begin
  111.     DestPtr := Ptr ($A000,Offset);
  112.     ExtractLineASM (BytesWide,Buf,DestPtr);
  113.     Inc (Offset,320);
  114.     end;
  115. end;
  116. { end PCX stuff }
  117.  
  118. Procedure Graph13h; assembler;
  119. asm
  120.   mov al,$13
  121.   mov ah,0
  122.   int 10h
  123. end;
  124.  
  125. VAR
  126.   F: File;           { PCX file }
  127.   Hdr: PCXHeaderPtr; { PCX header structure & file }
  128.   Pal: PalettePtr;   { PCX palette }
  129.   Shade, Size: Word; { RGB shade, file size }
  130.  
  131. BEGIN
  132.   Graph13h;                          { set mode 13h }
  133.   Assign(F, 'filename.pcx');         { open PCX file }
  134.   Reset(F,1);
  135.   Size := FileSize(F);
  136.   GetMem(Hdr, Size);                 { load PCX into memory }
  137.   Blockread(F, Hdr^, Size);
  138.   Close(F);
  139.   Pal := Ptr( Seg(Hdr^), Ofs(Hdr^) + Size - 768);    { get palette location }
  140.   Port[968] := 0;                                    { set palette }
  141.   FOR Shade := 0 TO 767 DO
  142.     Port[969] := Pal^[Shade] SHR 2;
  143.   DisplayPCX(0, 0, Hdr);                             { decode PCX to screen }
  144.   WHILE Readkey <> #13 DO;                           { wait for return key }
  145.   TextMode(CO80);
  146. END.
  147.